/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template  
 */
package com.apache.info.template;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.SpringContextLoader;
import com.apache.info.manager.PubMetadataManager;
import com.apache.info.manager.PubSortInfoManager;
import com.apache.info.template.helper.ActionTempManager;
import com.apache.info.template.helper.ConfigTempManager;
import com.apache.info.template.helper.EntityTempManager;
import com.apache.info.template.helper.OtherTempManager;
import com.apache.info.template.helper.PageTempManager;
import com.apache.info.util.FileOperate;
import com.apache.uct.common.ToolsUtil;

/**
 * description: 生成sql  
 * @author dyhou 创建时间：2016-9-29  
 * @Copyright(c)2014:北京中百信软件技术有限公司
 */
public class BuildProjectFactory {

	private Logger log = Logger.getLogger(getClass());
	private PubSortInfoManager sortInfoManager;// = LoadManagerFactory.getInstance().getManager("systemModelManager");
	private PubMetadataManager metadataManager;
	private static BuildProjectFactory instance;
	private Map<String, TemplateManager> temps = new HashMap<String, TemplateManager>();

	private BuildProjectFactory() {
		sortInfoManager = (PubSortInfoManager) SpringContextLoader.getBean("pubSortInfoManager");
		metadataManager = (PubMetadataManager) SpringContextLoader.getBean("pubMetadataManager");
		temps.put("pageTemp", new PageTempManager(sortInfoManager, metadataManager));
		temps.put("actionTemp", new ActionTempManager(sortInfoManager, metadataManager));
		temps.put("configTemp", new ConfigTempManager(sortInfoManager, metadataManager));
		temps.put("otherTemp", new OtherTempManager(sortInfoManager, metadataManager));
		temps.put("entityTemp", new EntityTempManager(sortInfoManager, metadataManager));
	}

	public static BuildProjectFactory instance() {
		if (null == instance) {
			instance = new BuildProjectFactory();
		}
		return instance;
	}

	@SuppressWarnings("rawtypes")
	public boolean ctreateProject(Map<String, String> param) {
		String createTemplate = param.get("createTemplate");
		boolean mark = false;
		Map<String, Object> map = new HashMap<String, Object>();
		map.putAll(param);
		ParamsVo vo = new ParamsVo();
		vo.setParams("sortIds", param.get("ids"));
		List list = (List) sortInfoManager.getList(vo);
		if (ToolsUtil.isEmpty(list)) {
			log.info("没有获取到数据表信息");
			return mark;
		}
		map.put("sortList", list);
		if ("0".equals(createTemplate)) {//全部
			mark = temps.get("pageTemp").createPage(map);
			//mark = temps.get("actionTemp").createAction(map);
			mark = temps.get("entityTemp").createEntity(map);
			mark = temps.get("otherTemp").createOther(map);
			mark = temps.get("configTemp").createConfig(map);
			otherInfo(map);
		} else if ("1".equals(createTemplate)) {//页面
			mark = temps.get("pageTemp").createPage(map);
		} else if ("2".equals(createTemplate)) {//配置文件
			mark = temps.get("configTemp").createConfig(map);
		} else if ("3".equals(createTemplate)) {//action
			//mark = temps.get("actionTemp").createAction(map);
		} else if ("4".equals(createTemplate)) {//entity
			mark = temps.get("entityTemp").createEntity(map);
		} else if ("5".equals(createTemplate)) {//其他
			mark = temps.get("otherTemp").createOther(map);
		}
		return mark;
	}

	/**
	 * description:  生成工程配置文件
	 */
	private void otherInfo(Map param) {
		String jarPath = String.valueOf(param.get("packPath"));
		String prefix = String.valueOf(param.get("projectName")) + "/";
		String xmlPath = String.valueOf(param.get("filePath")) + "/" + prefix;
		FileOperate.getInstance().newCreateFolder(xmlPath + "/");
		FileOperate.getInstance().newCreateFolder(xmlPath + "/src/site/");
		FileOperate.getInstance().newCreateFolder(xmlPath + "/src/test/resources/");
		FileOperate.getInstance().newCreateFolder(xmlPath + "/src/test/java/");
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		data.put("projectName", String.valueOf(param.get("projectName")));
		try {
			String result = FreemarkerHelper.instance().getTemplateStr("xml/pom.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "pom.xml", result);
			data.put("bodyReports", "${reports}");
			result = FreemarkerHelper.instance().getTemplateStr("xml/site.inc", data);
			FileOperate.getInstance().newCreateFile(xmlPath + "src/site/site.xml", result);
		} catch (Exception e) {
			log.error("生成xml文件失败", e);
		}
	}
}
